<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:directive.page import="java.io.File"/>
<jsp:directive.page import="java.util.List"/>
<jsp:directive.page import="java.util.Iterator"/>
<jsp:directive.page import="com.myzion.core.common.util.Constants"/>
<jsp:directive.page import="org.apache.commons.lang.StringUtils"/>
<jsp:directive.page import="org.apache.commons.fileupload.FileItem"/>
<jsp:directive.page import="org.apache.commons.fileupload.servlet.ServletFileUpload"/>
<jsp:directive.page import="org.apache.commons.fileupload.disk.DiskFileItemFactory;"/>

<%
String message = "";
String fPath = request.getParameter("filePath");
String inputFileName = request.getParameter("inputFileName");
String inputFileSize = request.getParameter("inputFileSize");
String showArea = request.getParameter("showArea");
fPath = (fPath == null ? "" : fPath);
%>
<%
String fileName = "";
if(StringUtils.isNotEmpty(request.getParameter("todo"))) {

  //上传文件的目录
    String filePath = Constants.getUploadPath(fPath);
  String uploadPath = application.getRealPath(filePath) + "/";  
  try {
    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload fileUpload = new ServletFileUpload(factory);
    // 设置最大文件尺寸，这里是3MB
    fileUpload.setSizeMax(3*1024*1024);
    // 设置临时目录：
    fileUpload.setHeaderEncoding("UTF-8");
    
    //得到所有的文件
    List fileItems = fileUpload.parseRequest(request);
    Iterator iter = fileItems.iterator();
    
    //如果文件目录不存在，做目录
    File modelDir = new File(uploadPath);
      if (!modelDir.exists()) {
          modelDir.mkdirs();
      }
      
      //依次处理每一个文件
    while (iter.hasNext()) {
  FileItem item = (FileItem) iter.next();
  if (!item.isFormField()) {
    fileName = item.getName();
    long size = item.getSize();
    if (StringUtils.isEmpty(fileName) && size == 0) {
      continue;
    }
    int len = fileName.lastIndexOf("\\");
    fileName = fileName.substring(len + 1);
    File temp = new File(uploadPath + fileName);
    if(!temp.exists()) {
      item.write(temp);
    } else {
      message = "文件上传失败！ <b>["+fileName+"]</b>此文件已经存在！";
      break;
    }
  }
    }
  } catch(Exception e) {
    message = "文件上传失败，请重试上传！";
  }
  
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 
<title>File Upload</title>

<script type="text/javascript">
function setValue() {
  <%if(fileName.toString().length() > 1) {%>
  
    var obj = window.opener.document;
    var nameObj = '<%=inputFileName%>';
    var sizeObj = '<%=inputFileSize%>';
    var showObj = '<%=showArea%>';
  
    var fnObj = obj.all[nameObj];
    var fsObj = obj.all[sizeObj];
    var saObj = obj.all[showObj];
    
    var fnVal = '<%=fileName.toString().substring(0, fileName.toString().length())%>';
        
    if(typeof(fnObj) != 'undefined') {
      if(fnObj.value != '') {
        fnObj.value = fnObj.value + ":" + fnVal;
      } else {
        fnObj.value = fnVal;
      }
    }
    
        var val = '<span align="center" style="width:300px; height:200px; border:#e3e3e3 1px solid; margin-right:5px; margin-top:5px; float:left;">';
        val = val + '<img src="/upload/blog/${userInfo.employee.enName}/<%=fileName%>"  width="100" height="100" /><br/>';
        val = val + '图片名称：<input type="text" name="attachment.extendFileName" /><br/>';
        val = val + '图片描述：<input type="text" name="attachment.description" /><br/>';
        val = val + '编辑 <input type="submit" value="保存" onclick="validate()"> 删除</span>';
    saObj.innerHTML = saObj.innerHTML + val;
    window.close();
  <%}%>
}
function isPhoto(){
      var photoName=document.getElementById('upload').value;
      var startNum=photoName.indexOf(".");
	      if(photoName.substring(startNum).toLowerCase()!=".gif"){
		       if(photoName.substring(startNum).toLowerCase()!=".jpg"){
			       if(photoName.substring(startNum).toLowerCase()!=".bmp"){
			        document.getElementById('upload').value=" ";
			      	document.getElementById('upload').focus();
			      	alert('上传的图片格式不正确！请重新选择！');
			      	return;
			       }
		       }
	      }
     }
</script>
</head>

<body onLoad="setValue()">
<br/>
<p style="font-weight:bold; font-size:20px; text-align:center">文件上传</p>
<p><font color="red"><%=message%></font>
<form name="uploadForm" action="handlePhoto.jsp?todo=upload&filePath=<%=fPath%>&inputFileName=<%=inputFileName%>&inputFileSize=<%=inputFileSize%>&showArea=<%=showArea%>" method="post" enctype="multipart/form-data">
  <ul style="list-style-type:none;">
    <li>选择您要上传的图片：<input type="file" name="upload" size="60" id="upload" onchange="isPhoto();"/></li>
    <li><span style="font-size:9pt; color:#ff0000;">*单个附件不能大于3M</span></li>
  </ul>
  
  <p style="text-align:center">
    <input name="button" type="submit" value="上 传" onclick="setValue()"/>
    <input name="button2" type="button" onClick="window.close()" value="取 消"/>
  </p>
</form>
</body>
</html>

